package com.chen.service.impl;


import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.entity.OperationLog;
import com.chen.mapper.OperationLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 操作日志服务
 */
@Service
public class OperationLogServiceImpl {


    @Autowired
    private OperationLogMapper operationLogMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 分页
     *
     * @param page
     * @param size
     * @param optModule
     * @param requestMethod
     * @return
     */
    public Page<OperationLog> findPage(int page, int size, String optModule, String requestMethod) {

//       根据id1进行倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        Query query = new Query();
        Criteria optModule1 = Criteria.where("optModule").regex(optModule);
        Criteria requestMethod1 = Criteria.where("requestMethod").regex(requestMethod);
        if (!"".equals(requestMethod)) {
            query.addCriteria(requestMethod1);
        }
        if (!"".equals(optModule1)) {
            query.addCriteria(optModule1);
        }
        long count = mongoTemplate.count(query, OperationLog.class);
        List<OperationLog> list = mongoTemplate.find(query.with(pageRequest), OperationLog.class);
        return new PageImpl(list, pageRequest, count);


    }


}
